using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using ECommerce.Library.DataStructure;

namespace AutoGear.BLL
{
    public class ShoppingCart
    {
        private ReadWriteCustomCollection<GiftCard> _giftCard;
        public ReadWriteCustomCollection<GiftCard> GiftCards
        {
            get { return _giftCard; }
            set { _giftCard = value; }
        }

        private ReadWriteCustomCollection<CartItem> _cartItems;
        public ReadWriteCustomCollection<CartItem> CartItems
        {
            get { return _cartItems; }
            set { _cartItems = value; }
        }

        private ReadWriteCustomCollection<GiftItem> _giftItems;
        public ReadWriteCustomCollection<GiftItem> GiftItems
        {
            get { return _giftItems; }
            set { _giftItems = value; }
        }

        private ReadWriteCustomCollection<MultipleShipAdd> _multipleShipAdd;
        public ReadWriteCustomCollection<MultipleShipAdd> MultipleShipAdd 
        {
            get { return _multipleShipAdd; }
            set { _multipleShipAdd = value; }
        }

        private double _totalWeight;
        public double TotalWeight
        {
            get
            {
                _totalWeight = 0;
                for (int i = 0; i < this._cartItems.Count; i++)
                {

                    ReadWriteCustomCollection<ProductStock> stocks = ProductStock.GetStockByProductId(this._cartItems[i].Product.ProductId);
                    if (stocks != null && stocks.Count > 0)
                    {
                        _totalWeight = _totalWeight + stocks[0].Weight;
                    }

                }
                return _totalWeight;
            }
        }

        private double _subTotal;
        public double SubTotal
        {
            get
            {
                _subTotal = 0.0;
                for (int i = 0; i < this._cartItems.Count; i++)
                {
                    _subTotal = _subTotal + this._cartItems[i].ItemTotal;
                }
                    return _subTotal;
            }
        }

        private Int32 _TotalCartItem;
        public Int32 TotalCartItem
        {
            get
            {
                _TotalCartItem = 0;
                for (int i = 0; i < this._cartItems.Count; i++)
                {
                    _TotalCartItem = _TotalCartItem + this._cartItems[i].Quantity;
                }
                return _TotalCartItem;
            }
        }

        private Int32 _TotalGiftItem;
        public Int32 TotalGiftItem
        {
            get
            {
                _TotalGiftItem = 0;
                for (int i = 0; i < this._giftItems.Count; i++)
                {
                    _TotalGiftItem = _TotalGiftItem + this._giftItems[i].Quantity;
                }
                return _TotalGiftItem;
            }
        }

        private double _giftSubTotal;
        public double GiftSubTotal
        {
            get
            {
                _giftSubTotal = 0.0;
                for (int i = 0; i < this._giftItems.Count; i++)
                {
                    _giftSubTotal = _giftSubTotal + this._giftItems[i].ItemTotal;
                }
                return _giftSubTotal;
            }
        }

        public ShoppingCart()
        {
            this._cartItems = new ReadWriteCustomCollection<CartItem>();
            this._giftItems = new ReadWriteCustomCollection<GiftItem>();
            this._giftCard = new ReadWriteCustomCollection<GiftCard>();
            this._multipleShipAdd = new ReadWriteCustomCollection<MultipleShipAdd>();
        }

        public CartItem FindCartItemByItemNumber(string ItemNumber)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void AddGiftCard(GiftCard giftCard)
        {
            this.GiftCards.Add(giftCard);
        }

        public void AddCartItem(CartItem cartItem)
        {
            for (int i = 0; i < this._cartItems.Count; i++)
            {
                if (this._cartItems[i].Product.ProductId == cartItem.Product.ProductId)
                {
                    if (this._cartItems[i].Quantity + cartItem.Quantity > SiteUtility.MaximumCartItemQuantity)
                    {
                        this._cartItems[i].Quantity = SiteUtility.MaximumCartItemQuantity;
                    }
                    else
                    {
                        this._cartItems[i].Quantity = this._cartItems[i].Quantity + cartItem.Quantity;
                    }
                    this._cartItems[i].MetalType = cartItem.MetalType;
                    this._cartItems[i].GiftWrapper = cartItem.GiftWrapper;
                    return;
                }
            }
            this._cartItems.Add(cartItem);
        }
        public void UpdateCartItem(CartItem cartItem)
        {
            for (int i = 0; i < this._cartItems.Count; i++)
            {
                if (this._cartItems[i].Product.ProductId == cartItem.Product.ProductId)
                {
                    this._cartItems[i] = cartItem;
                    break;
                }
            }
        }
        public void AddGiftItem(GiftItem giftItem)
        {
            for (int i = 0; i < this.GiftItems.Count; i++)
            {
                if (this.GiftItems[i].GiftID == giftItem.GiftID)
                {
                    if (this.GiftItems[i].Quantity + giftItem.Quantity > SiteUtility.MaximumCartItemQuantity)
                    {
                        this.GiftItems[i].Quantity = SiteUtility.MaximumCartItemQuantity;
                    }
                    else
                    {
                        this.GiftItems[i].Quantity = this.GiftItems[i].Quantity + giftItem.Quantity;
                    }
                    return;
                }
            }
            this.GiftItems.Add(giftItem);
        }
        public void UpdateGiftItem(GiftItem giftItem)
        {
            for (int i = 0; i < this.GiftItems.Count; i++)
            {
                if (this.GiftItems[i].GiftID == giftItem.GiftID)
                {
                    this.GiftItems[i] = giftItem;
                    break;
                }
            }
        }
        public GiftItem FindGiftItemByGiftId(string giftId)
        {
            for (int i = 0; i < this.GiftItems.Count; i++)
            {
                if (this.GiftItems[i].GiftID.Equals(giftId))
                {
                    return this.GiftItems[i];
                }
            }

            return null;
        }


        public void AddMultipleShipAdd(MultipleShipAdd multipleShipp)
        {
            //for (int i = 0; i < this._multipleShipAdd.Count; i++)
            //{
            //    this._multipleShipAdd[i].ShippAdd = multipleShipp.ShippAdd;
            //    this._multipleShipAdd[i].ProductAdditional = multipleShipp.ProductAdditional;
            //}
            this._multipleShipAdd.Add(multipleShipp);
        }
    }
}
